package com.codingfly.core.util;

import com.codingfly.core.exception.AuthenticationException;
import org.springframework.util.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

public class TokenUtils {
    public static String getAccessToken() {
        HttpServletRequest request = ((ServletRequestAttributes) (RequestContextHolder.currentRequestAttributes())).getRequest();
        String accessToken = request.getHeader("accessToken");
        if (StringUtils.isEmpty(accessToken)) {
            return request.getParameter("accessToken");
        }
        return accessToken;
    }

    public static String getAccessTokenThrowErr() {
        String accessToken = getAccessToken();
        if (StringUtils.isEmpty(accessToken)) {
            throw new AuthenticationException();
        }
        return accessToken;
    }

}